package com.mxfcode.front.bbs_plate.controller;

import com.mxfcode.bbs_common.util.community.CommunityConstant;
import com.mxfcode.bbs_common.util.page.Page;
import com.mxfcode.entity.BBSArticle;
import com.mxfcode.entity.BBSPlate;
import com.mxfcode.entity.BBSPlate;
import com.mxfcode.entity.TSUser;
import com.mxfcode.front.bbs_article.service.ArticleService;
import com.mxfcode.front.bbs_login.service.LoginService;
import com.mxfcode.front.bbs_plate.service.PlateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PlateController
 * @author moxiufeng
 * @date 2021/2/27 23:03
 */
@Controller
@RequestMapping("/plate")
public class PlateController implements CommunityConstant {
    
    @Autowired
    private PlateService plateService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private LoginService loginService;
    
    @GetMapping(value = "/list")
    public String toPlateListPage(Model model, Page page){
        page.setRows(plateService.selectPlateRows(1));
        page.setPath("/plate/list");
        page.setLimit(6);
        List<BBSPlate> dataList = plateService.selectPlateList(page.getOffset(), page.getLimit());
        model.addAttribute("plates",dataList);
        return "/plate";
    }

    @RequestMapping(path = "/article/list",method = RequestMethod.GET)
    public String goPlateArticleListPage(String id,Model model, Page page){
        BBSPlate plate = plateService.selectPlateById(id);
        List<BBSArticle> list = articleService.selectBBSArticleByPlateId(id);
        List<Map<String,Object>> dataList = new ArrayList<>();
        if(list != null){
            for (BBSArticle article : list ){
                Map<String,Object> map = new HashMap<>();
                map.put("article",article);
                String userId = article.getUserId();
                TSUser user = loginService.findUserById(userId);
                map.put("user",user);
                dataList.add(map);
            }
        }
        model.addAttribute("plate",plate);
        model.addAttribute("articles",dataList);
        return "/plate_article";
    }
}
